package com.leetcode.LC;

import java.util.Arrays;

public class LC136 {
    public int singleNumber(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        }

        Arrays.sort(nums);
        if (nums[0] != nums[1]) {
            return nums[0];
        } else if (nums[nums.length - 1] != nums[nums.length - 2]) {
            return nums[nums.length - 1];
        }

        for (int i = 1; i < nums.length - 1; i++) {
            if (nums[i] != nums[i - 1] && nums[i] != nums[i + 1]) {
                return nums[i];
            }
        }

        return -1;

    }

    public static void main(String[] args) {
        System.out.println(new LC136().singleNumber(new int[]{4, 1, 2, 1, 2}));
    }
}
